<?php
/**
 * 活动管理
 * @author cjli
 *
 */
class ActivityAction extends AdminAction
{
	public function _initialize()
	{
		parent::_initialize();
	}

    //首页
	public function index(){

	}    

    //秒杀列表
	public function seckill(){
        $pageRows = 15;
        $seckill_db = D('seckill');
        $count = $seckill_db->count();
        import("ORG.Util.Page");
        $Page = new Page($count,$pageRows);
        $pageShow = $Page->show();
        $list = $seckill_db->limit($Page->firstRow.','.$Page->listRows)->order('start_time desc ')->select();

        $nowtime = time();
        foreach($list as $key=>$val){
            $count_where['fid'] = $val['id'];
            $count = D('seckill_items')->field('count(*) as pcount, sum(qty) as pnum')->where($count_where)->find();

            $list[$key]['pcount'] = $count['pcount'];
            $list[$key]['pnum']   = $count['pnum'];
            //计算秒杀状态
            $start_time =  $val['start_time'];
            $end_time =  $val['end_time'];
            if($nowtime < $start_time){
                $list[$key]['state'] = 0;
                $list[$key]['statestr'] = '<font color="green">未开始</font>';
            }else if($nowtime >= $start_time && $nowtime <= $end_time){
                $list[$key]['state'] = 1;
                $list[$key]['statestr'] = '<font color="red">正在秒杀中</font>';
            }else{
                $list[$key]['state'] = 2;
                $list[$key]['statestr'] = '已结束';
            }
        }

        $this->assign('pageShow', $pageShow);
        $this->assign('language',$language);
        $this->assign('list',$list);
        $this->display('seckill');
    }

    //秒杀管理 
	public function seckill_pro(){
        
        if($_REQUEST['add']){
            //添加秒杀活动
            if($_POST['dosubmit']){
                //开始时间
                $start_time = strtotime($_POST['start_time']); 
                $end_time_i = $_POST['end_time_i'];
                $end_time_s = $_POST['end_time_s'];
                //结束时间
                $end_time   = $start_time + $end_time_i*60 + $end_time_s;
                
                $seckill_db = D('seckill');

                //查询是否符合时间条件，当前时间端没有秒杀活动
                $timeVerify = ActivityModel::timeVerify($start_time, $end_time, 0);
                if(!$timeVerify){
                    //写入主表
                    $add_data['start_time'] = $start_time;
                    $add_data['end_time']   = $end_time;
                    $MAXid = $seckill_db->getField('MAX(id)'); 
                    $sn_str = 'MS000000';
                    $MAXid += 1;
                    $add_data['seckill_sn'] =  substr($sn_str, 0, strlen($sn_str)-strlen(strval($MAXid))).$MAXid;
                    $fid = $seckill_db->add($add_data);
                    if(!$fid) $this->error('抱歉！秒杀活动数据写入失败，请重新添加！', U('Activity/seckill_pro'));
                    //写入子表
                    $pid = $_POST['id'];
                    $qty = $_POST['qty'];
                    $price = $_POST['price'];
                    $seckill_items_db = D('seckill_items');
                    foreach($pid as $key=>$val){
                        if(intval($val)>0){
                            $add_data = array(
                                'fid'   => $fid, 
                                'pid'   => $val,
                                'qty'   => $qty[$key],
                                'price' => $price[$key],
                            );
                            $seckill_items_db -> add($add_data);
                        }
                    }

                    $this->success('^_^ 添加成功! 即将跳回列表页，请稍后!', U('Activity/seckill'));
                    
                }else{
                    $this->error('抱歉！时间重复，请重新添加！', U('Activity/seckill_pro'));
                }
                exit;
            }
            $this->display('seckill_form');  exit;
        }

        if($_REQUEST['edit']){

            $fid = $_REQUEST['fid'];
            $seckill_db = D('seckill');

            //修改秒杀活动
            if($_POST['dosubmit']){
                //开始时间
                $start_time = strtotime($_POST['start_time']); 
                $end_time_i = $_POST['end_time_i'];
                $end_time_s = $_POST['end_time_s'];
                //结束时间
                $end_time   = $start_time + $end_time_i*60 + $end_time_s;
                
                //查询是否符合时间条件，当前时间端没有秒杀活动
                $timeVerify = ActivityModel::timeVerify($start_time, $end_time, $fid);
                if(!$timeVerify){
                    //写入主表
                    $add_data['start_time'] = $start_time;
                    $add_data['end_time']   = $end_time;
                    $res = $seckill_db->where(array('id' => $fid ))->save($add_data);
                           $seckill_db->where(array('id' => $fid ))->setInc('versions');       
                    //写入子表
                    $pid = $_POST['id'];
                    $qty = $_POST['qty'];
                    $price = $_POST['price'];
                    $oldid = $_POST['oldid'];
                    $seckill_items_db = D('seckill_items');
                    foreach($pid as $key=>$val){
                        if($oldid[$key]){
                            $edit_data = array(
                                'pid'   => $val,
                                'qty'   => $qty[$key],
                                'price' => $price[$key],
                            );
                            $seckill_items_db ->where(array('id' => $oldid[$key]))->save($edit_data);
                        }else{
                            $add_data = array(
                                'fid'   => $fid, 
                                'pid'   => $val,
                                'qty'   => $qty[$key],
                                'price' => $price[$key],
                            );
                            $seckill_items_db -> add($add_data);
                        }
                    }
                    $oldiddel = $_POST['oldiddel'];
                    if($oldiddel){
                        $oldiddel_arr = explode(',', $oldiddel);
                        foreach($oldiddel_arr as $key=>$val){
                            if( intval($val) > 0 ) $seckill_items_db ->where(array('id' => $val))->delete();
                        }
                    }
                    $this->success('^_^ 修改成功! 即将跳回列表页，请稍后!', U('Activity/seckill')); 
                }else{
                    $this->error('抱歉！时间重复，请重新添加！', U('Activity/seckill_pro', 'edit=1&fid='.$fid));
                }
                exit;
            }

            $seckill = $seckill_db->where(array('id'=>$fid))->find();
            $ss = $seckill['end_time'] - $seckill['start_time'];
            $seckill['i'] = floor($ss/60);
            $seckill['s'] = $ss - $seckill['i']*60;  

            $seckill_items_db = D('seckill_items');
            $seckill_items = $seckill_items_db->where(array('fid'=>$fid))->select();

            $this->assign('seckill',$seckill);
            $this->assign('seckill_items',$seckill_items);
            $this->display('seckill_form');  exit;
        }

        if($_REQUEST['del']){

            $fid = $_REQUEST['fid'];
            D('seckill') ->where(array('id' => $fid))->delete();     
            D('seckill_items') ->where(array('fid' => $fid))->delete();

            $this->success('^_^ 删除成功! 即将跳回列表页，请稍后!', U('Activity/seckill'));
            exit;
        }

        if($_REQUEST['timeverify']){

            $fid = $_REQUEST['fid'];
            //开始时间
            $start_time = strtotime($_POST['start_time']); 
            $end_time_i = $_POST['end_time_i'];
            $end_time_s = $_POST['end_time_s'];
            //结束时间
            $end_time   = $start_time + $end_time_i*60 + $end_time_s;

            $seckill_db = D('seckill');
            //查询是否符合时间条件，当前时间端没有秒杀活动
            $result = ActivityModel::timeVerify($start_time, $end_time, $fid);
            if(!$result){
                echo 1;
            }else{
                $str = "与以下活动的时间有冲突，请重新规划时间\r\n";
                $where['_string']  = ' (start_time <= '.$start_time.' and end_time >= '.$start_time.')';
                $start = $seckill_db->field('seckill_sn, start_time, end_time')->where($where)->find();
                if($start){
                   $str .= "活动编号:".$start['seckill_sn']." 开始时间:".date('Y-m-d H:i:s',$start['start_time'])." 结束时间:".date('Y-m-d H:i:s',$start['end_time']);   
                }
                $where['_string']  = ' (start_time <= '.$end_time.' and end_time >= '.$end_time.')';
                $end = $seckill_db->field('seckill_sn, start_time, end_time')->where($where)->find();
                if($end && $start != $end){
                   $str .= "\r\n活动编号:".$end['seckill_sn']." 开始时间:".date('Y-m-d H:i:s',$end['start_time'])." 结束时间:".date('Y-m-d H:i:s',$end['end_time']);   
                }
                echo $str;                            
            }
            exit;
        }

    }
	//秒杀活动结果
    public function seckill_result(){
        $pageRows =15;
        $seckillResult_db = D('seckill_result');
        $count = $seckillResult_db->count();
        import("ORG.Util.Page");
        $Page = new Page($count,$pageRows);
        $pageShow = $Page->show();       
        $seckillResult_db->limit($Page->firstRow.','.$Page->listRows);
		$list = $seckillResult_db->join('users ON users.id = seckill_result.user_id')->join('seckill ON seckill.id = seckill_result.fid')	
		->order('time desc')->select();
        foreach($list as $key=>$value){
        
            $list[$key]['pid'] = ProductModel::get_product_info($value['pid']);
           
        }
		//dump($list);exit;
        $this->assign('list',$list);
        $this->assign('pageShow', $pageShow);
        $this->display('seckill_result');
    }
//圣诞节活动
	public function christmas(){
        $pageRows =15;
        $christmas_db = D('christmas_result as result');
        $count = $christmas_db->count();
        import("ORG.Util.Page");
        $Page = new Page($count,$pageRows);
        $pageShow = $Page->show();       
        $christmas_db->limit($Page->firstRow.','.$Page->listRows);

		$list = $christmas_db->join('users as users on(result.user_id=users.id)')->order('time DESC')->select();
		
		// echo $christmas_db->getLastSql();exit;
        $this->assign('list',$list);		
        $this->assign('pageShow', $pageShow);
		$this->display('christmas_result');
	}
	//大转盘活动
	public function fortunewheel()
	{
	    $pageRows =15;
        $fortunewheel_db = D('fortunewheel as wheel');
        $count = $fortunewheel_db->count();
        import("ORG.Util.Page");
        $Page = new Page($count,$pageRows);
        $pageShow = $Page->show();       
        $fortunewheel_db->limit($Page->firstRow.','.$Page->listRows);

		$list = $fortunewheel_db->join('users as users on(wheel.user_id=users.id)')->order('time DESC')->select();
		// echo $christmas_db->getLastSql();exit;
        $this->assign('list',$list);		
        $this->assign('pageShow', $pageShow);
		$this->display('fortunewheel_result');	
	}

}